class ReservasController < ApplicationController
  # GET /reservas
  # GET /reservas.json
  def index
    @reservas = Reserva.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @reservas }
    end
  end

  # GET /reservas/1
  # GET /reservas/1.json
  def show
    @reserva = Reserva.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @reserva }
    end
  end

  # GET /reservas/new
  # GET /reservas/new.json
  def new
    @reserva = Reserva.new
    @reserva.funcionario_id = current_user.id
    @reserva.hospede_id = params[:hospede_id]
    gera_lista_quartos

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @reserva }
    end
  end

  # GET /reservas/1/edit
  def edit
    @reserva = Reserva.find(params[:id])
    gera_lista_quartos
  end

  # POST /reservas
  # POST /reservas.json
  def create
    @reserva = Reserva.new(params[:reserva])

    @reserva.funcionario_id = current_user.id
    @reserva.hospede_id = params[:reserva][:hospede_id]
    @reserva.quarto_id = params[:reserva][:quarto_id]

    respond_to do |format|
      if @reserva.save
        format.html { redirect_to @reserva, notice: 'Reserva criada com sucesso.' }
        format.json { render json: @reserva, status: :created, location: @reserva }
      else
        gera_lista_quartos
#       flash[:error] = "Nao pode fazer reserva - #{params[:reserva]}"
        format.html { render action: "new" }
        format.json { render json: @reserva.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /reservas/1
  # PUT /reservas/1.json
  def update
    @reserva = Reserva.find(params[:id])

    respond_to do |format|
      if @reserva.update_attributes(params[:reserva])
        format.html { redirect_to @reserva, notice: 'Reserva atualizada com sucesso.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @reserva.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /reservas/1
  # DELETE /reservas/1.json
  def destroy
    @reserva = Reserva.find(params[:id])
    @reserva.destroy

    respond_to do |format|
      format.html { redirect_to reservas_url }
      format.json { head :no_content }
    end
  end

  def confirmar_reserva
    reserva = Reserva.find(params[:reserva_id])
    reserva.data_encerramento = Time.now

    if reserva.save
      #redirect_to from_reserva_path, :reserva_id => reserva.id
      redirect_to from_reserva_path(reserva.id)
    else
      flash[:error] = "Nao foi possivel confirmar a reserva"
      render :action => "index"
    end
  end

  private
  def gera_lista_quartos
    quartos = Quarto.all
    @lista_quartos = []
    
    quartos.each do |q|
      @lista_quartos << [q.numero.to_s, q.id]
    end
  end

  private
  def erro_validacao(m)
    gera_lista_quartos
    flash[:error] = m
    redirect_to usuario_path(@reserva.hospede)
  end
end
